<?php

/**
 * This is the model class for table "{{stockgroup}}".
 *
 * The followings are the available columns in table '{{stockgroup}}':
 * @property string $idstockgroup
 * @property string $stockgroupname
 * @property string $parent
 *
 * The followings are the available model relations:
 * @property Receive[] $receives
 * @property Stockgroup $parent0
 * @property Stockgroup[] $stockgroups
 * @property Stockitems[] $stockitems
 */
class Stockgroup extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Stockgroup the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{stockgroup}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('stockgroupname', 'required'),
            array('stockgroupname', 'length', 'max' => 45),
            array('parent', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idstockgroup, stockgroupname, parent', 'safe', 'on' => 'search'),
            array('parentname', 'autocompleteValidatorSelf', 'model' => 'Stockgroup'),            
            //array('stockgroupname', 'autocompleteValidator', 'model' => 'Stockgroup'),
        );
    }

    public function autocompleteValidatorSelf($attribute, $params) {
        $match = addcslashes($_REQUEST[$attribute], '%_');
        if (!$params['model']::model()->exists(new CDbCriteria(array(
                            'condition' => "stockgroupname = :stockgroupname",
                            'params' => array(":stockgroupname" => "$match")
                        )))) {
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
        }
    }

    public function autocompleteValidator($attribute, $params) {
        $match = addcslashes($_REQUEST[$attribute], '%_');
        if (!$params['model']::model()->exists(new CDbCriteria(array(
                            'condition' => "$attribute = :$attribute",
                            'params' => array(":$attribute" => "$match")
                        )))) {
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
        }
    }

    public function beforeSave() {
        if (isset($_REQUEST['parentname'])) {
            $match = addcslashes($_REQUEST['parentname'], '%_'); // escape LIKE's special characters
            $criteria = new CDbCriteria(array(
                        'condition' => 'stockgroupname = :stockgroupname',
                        'params' => array(':stockgroupname' => "$match")
                            ));
            $model = Stockgroup::model()->find($criteria);
            $this->parent = $model->idstockgroup;
        }
        return parent::beforeSave();
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'receives' => array(self::HAS_MANY, 'Receive', 'pono'),
            'parent0' => array(self::BELONGS_TO, 'Stockgroup', 'parent'),
            'stockgroups' => array(self::HAS_MANY, 'Stockgroup', 'parent'),
            'stockitems' => array(self::HAS_MANY, 'Stockitems', 'idstockgroup'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idstockgroup' => Yii::t('application', 'Idstockgroup'),
            'stockgroupname' => Yii::t('application', 'Stockgroupname'),
            'parent' => Yii::t('application', 'Parent'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($idstockgroup=NULL) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        if($idstockgroup != NULL){            
            $criteria->compare('idstockgroup', '>='. $idstockgroup);
        }        
        else{
            $criteria->compare('idstockgroup', $this->idstockgroup, true);
        }
        $criteria->compare('stockgroupname', $this->stockgroupname, true);
        $criteria->compare('parent', $this->parent, true);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}